import smtplib
from email.header import Header
from email.mime.text import MIMEText

def sendEmail(receiver,yzm):
    # 第三方 SMTP 服务
    sender = "lwjidea@163.com"
    mail_host = "smtp.163.com"  # SMTP服务器
    mail_user = "lwjidea@163.com"  # 用户名
    mail_pass = "lwj123456"  # 授权密码，非登录密码
    receivers = [receiver]
    title = '新经新闻交流平台'
    content = '当前验证码为:%s,请勿告诉他人'%yzm  # 邮件主题
    message = MIMEText(content, 'plain', 'utf-8')  # 内容, 格式, 编码
    message['From'] = "{}".format(sender)
    message['To'] = ",".join(receivers)
    message['Subject'] = title
    try:
        smtpObj = smtplib.SMTP_SSL(mail_host, 465)  # 启用SSL发信, 端口一般是465
        smtpObj.login(mail_user, mail_pass)  # 登录验证
        smtpObj.sendmail(sender, receivers, message.as_string())  # 发送
        print("mail has been send successfully.")
    except smtplib.SMTPException as e:
        print(e)



if __name__ == '__main__':
    receiver = "lwjidea@126.com"
    yzm = "678524"
    sendEmail(receiver,yzm)